//
//  GZAFNetworkTool.swift
//  sinaweibo
//
//  Created by mac book pro on 16/3/21.
//  Copyright © 2016年 finish. All rights reserved.
//

import UIKit
import AFNetworking
/// 网络单例
class GZAFNetworkTool: NSObject {
    /// 单列默认的构造方法
    static let sharedNetworkTool = GZAFNetworkTool()
    
    private var afnManager: AFHTTPSessionManager
    
    //稀有化构造
    private  override init() {
        let url: NSURL? = NSURL(string: "https://api.weibo.com/")
        afnManager = AFHTTPSessionManager(baseURL: url)
        afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        super.init()
    }
    /**
    封装AFN  GET请求
    
    - parameter urlString:  url
    - parameter parameters: 参数
    - parameter callBack:   成功回调
    - parameter error:      失败回调
    */
    func requestGET(urlString:String,parameters: [String:AnyObject]?,callBack:(result:[String:AnyObject]?,error:NSError?)->()){
        afnManager.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            callBack(result:result as? [String:AnyObject], error: nil)
            
            }) { (_, error) -> Void in
                callBack(result: nil, error: error)
        }
        
    }
    /**
    封装AFN POST请求
    
    - parameter urlString:  url
    - parameter parameters: 参数
    - parameter callBack:   成功回调
    - parameter error:      失败回调
    */
    func requestPOST(urlString:String,parameters: [String:AnyObject]?,callBack:(result:[String:AnyObject]?,error:NSError?)->()){
        afnManager.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            callBack(result: result as? [String:AnyObject], error: nil)
            
            }) { (_, error) -> Void in
                callBack(result: nil, error: error)
        }
        
    }
}
